package world.criver.h50909.tool;

import org.springframework.stereotype.Component;
import world.criver.h50909.data.daSqlite;
import world.criver.h50909.entity.*;

import java.util.ArrayList;
import java.util.List;
@Component
public class toolModelInfo {
    /**
     * 机型概要列表
     * @return
     */
    public List<entModelSummary> summaryList(){
        try {
            daSqlite.lock.lock();
            List<entModelSummary> as= daSqlite.tool.query("select image,model,timestamp from model_info", entModelSummary.class);
            daSqlite.lock.unlock();
            return as;
        }catch (Exception e){
            daSqlite.lock.unlock();
            return new ArrayList<>();
        }
    }

    /**
     * 添加机型
     * @param info
     * @return
     */
    public boolean add(entEquipmentInfo info){
        daSqlite.lock.lock();
        boolean a= daSqlite.tool.update("insert into model_info values('"+info.getModel()+"','"+info.getImage()+"','"+info.getMethod()+"','"
                +info.getProtocol()+"','"+info.getAlarmKey()+"','"+info.getProcessKey()+"','"+info.getAwaitKey()+"','"+info.getProgramKey()+"','"
                +info.getSpeedKey()+"',"+info.getTimestamp()+")")>0;
        daSqlite.lock.unlock();
        return a;
    }

    /**
     * 更改机型
     * @param info
     * @param name
     * @return
     */
    public boolean update(entEquipmentInfo info,String name){
        daSqlite.lock.lock();
        boolean a= daSqlite.tool.update("update model_info set model='"+info.getModel()+"',image='"+info.getImage()+"',method='"+info.getMethod()+"',protocol='"
                +info.getProtocol()+"',alarmKey='"+info.getAlarmKey()+"',processKey='"+info.getProcessKey()+"',awaitKey='"+info.getAwaitKey()+"',programKey='"
                +info.getProgramKey()+"',speedKey='"+info.getSpeedKey()+"',timestamp="+info.getTimestamp()+" where model='"+name+"'")>0;
        daSqlite.lock.unlock();
        return a;
    }
    /**
     * 获取图片
     * @param name
     * @return
     */
    public String getImage(String name){
        daSqlite.lock.lock();
        List<entName> ims=daSqlite.tool.query("select image as name from model_info where model='"+name+"'", entName.class);
        daSqlite.lock.unlock();
        if(ims.isEmpty())return "equipment_normal";
        return ims.get(0).getName();
    }

    /**
     * 查询机型详情
     * @param name
     * @return
     */
    public entModelInfo info(String name){
        daSqlite.lock.lock();
        List<entModelInfo> as= daSqlite.tool.query("select * from model_info where model='"+name+"'",entModelInfo.class);
        daSqlite.lock.unlock();
        if(as.isEmpty())return new entModelInfo();
        return as.get(0);
    }

    /**
     * 删除机型
     * @param name
     * @return
     */
    public boolean delete(String name){
        daSqlite.lock.lock();
        boolean a= daSqlite.tool.update("delete from model_info where model='"+name+"'")>0;
        daSqlite.lock.unlock();
        return a;
    }
}
